# clear working environment
rm(list = ls())

#### load the required packages ####
library(tidyverse)
library(PanelMatch)
library(countrycode)

#### load the clean data ####
df <- readRDS("data/dem-transitions-replication-data.rds") %>%
  glimpse()

#### make df into a properly formatted dataframe for PanelMatch ####

# begin coercing df into m
# df needs to be a dataframe class
m <- as.data.frame(df)
# check to see if m is a dataframe
class(m)

# country needs to be an integer to use PanelMatch
m$country <- as.integer(m$country)

# year needs to be an integer to use PanelMatch
m$year <- as.integer(m$year)

# create placebo treatment variables
m <- m %>%
  group_by(country) %>%
  mutate(regime_lead_4 = dplyr::lead(v2x_regime, 4)) %>%
  mutate(regime_lead_6 = dplyr::lead(v2x_regime, 6)) %>%
  mutate(year_lead_4 = dplyr::lead(year, 4)) %>%
  mutate(year_lead_6 = dplyr::lead(year, 6)) %>%
  ungroup() %>%
  glimpse()

m$placebo <- as.numeric(ifelse(m$regime_lead_6 >= 2, 1, 0))

# all other variables, except categorical variables, need to be numeric
m$mid <- as.numeric(m$mid)
m$fatal <- as.numeric(m$fatal)
m$ln_cinc <- as.numeric(m$ln_cinc)
m$ln_pecpp <- as.numeric(m$ln_pecpp)
m$terr_dispute <- as.numeric(m$terr_dispute)
m$v2x_freexp_altinf <- as.numeric(m$v2x_freexp_altinf)
m$v2xlg_legcon <- as.numeric(m$v2xlg_legcon)
m$v2x_cspart <- as.numeric(m$v2x_cspart)
m$v2x_suffr <- as.numeric(m$v2x_suffr)

# make sure all of the rows are unique
m <- distinct(m, country, year, .keep_all = TRUE)

# df needs to be a dataframe class
m <- as.data.frame(m)
# check to see if m is a dataframe
class(m)

# set seed to reproduce bootstrapped estimates
set.seed(1)

# Placebo democratic transition on legislative constraints 
# create panel data
panel <- PanelData(panel.data = m,
                   unit.id = "country",
                   time.id = "year",
                   treatment = "placebo",
                   outcome = "v2xlg_legcon")

# create matched sets
PMresults.legcon.att <- PanelMatch(panel.data = panel, lag = 5, refinement.method = "CBPS.weight",
                                   match.missing = TRUE, exact.match.variables = c("un_region"),
                                   covs.formula = ~ I(lag(v2xlg_legcon, 1:5)) + I(lag(ln_cinc, 1:5)) + I(lag(ln_pecpp, 1:5)) + I(lag(terr_dispute, 1:5)),
                                   qoi = "att", lead = 1:5, 
                                   forbid.treatment.reversal = FALSE, use.diagonal.variance.matrix = TRUE)

# estimate the ATT
PEresults.legcon.att <- PanelEstimate(panel.data = panel, 
                                      sets = PMresults.legcon.att,
                                      se.method = "unconditional",
                                      confidence.level = .90)

# Placebo democratic transition on alternate information 
# create panel data
panel <- PanelData(panel.data = m,
                   unit.id = "country",
                   time.id = "year",
                   treatment = "placebo",
                   outcome = "v2x_freexp_altinf")

# create matched sets
PMresults.altinfo.att <- PanelMatch(panel.data = panel, lag = 5, refinement.method = "CBPS.weight",
                                    match.missing = TRUE, exact.match.variables = c("un_region"),
                                    covs.formula = ~ I(lag(v2x_freexp_altinf, 1:5)) + I(lag(ln_cinc, 1:5)) + I(lag(ln_pecpp, 1:5)) + I(lag(terr_dispute, 1:5)),
                                    qoi = "att", lead = 1:5, 
                                    forbid.treatment.reversal = FALSE, use.diagonal.variance.matrix = TRUE)

# estimate the ATT
PEresults.altinfo.att <- PanelEstimate(panel.data = panel, sets = PMresults.altinfo.att, 
                                       se.method = "unconditional",
                                       confidence.level = .90)

# create a plot 
gg <- data.frame(years = rep(c("t-5", "t-4", "t-3", "t-2", "t-1"), 2),
                 estimates = c(PEresults.altinfo.att[["estimate"]], PEresults.legcon.att[["estimate"]]),
                 st_error = c(PEresults.altinfo.att[["standard.error"]], PEresults.legcon.att[["standard.error"]]),
                 outcome = c(rep("Freedom of Expression (V-Dem)", 5), rep("Executive Constraints (V-Dem)", 5))) %>%
  mutate(lo_ci = estimates - (1.645*st_error)) %>%
  mutate(hi_ci = estimates + (1.645*st_error)) %>% 
  mutate(significant = as.factor(ifelse(lo_ci > 0 | hi_ci < 0, 1, 0))) %>%
  glimpse()

# create Figure G.2 for the appendix
ggplot(data = gg, aes(x = reorder(years, desc(years)), color = outcome, shape = outcome)) +
  geom_hline(aes(yintercept = 0), linetype = "dashed", linewidth = .25) +
  geom_pointrange(aes(y = estimates, ymin = lo_ci, ymax = hi_ci), size = .4, linewidth = .7, position = position_dodge(width = .2)) +
  scale_color_manual(values = c("black", "darkgray")) +
  ylim(-.05, .3) +
  theme_minimal() +
  theme(legend.position = "bottom",
        axis.text.x = element_text(color = "black", size = 10),
        axis.title.y = element_text(size = 12),
        legend.title = element_text(size = 12)) +
  labs(y = "Effect of (Placebo) Democratic Transition",
       x = "Years Until Actual Transition",
       color = "",
       shape = "")

# save plot
ggsave("figures/constraints-placebo-att-plot.pdf", width = 6.5, height = 5, units = "in", bg = "white")

# Placebo democratic transition on civil society 
# create panel data
panel <- PanelData(panel.data = m,
                   unit.id = "country",
                   time.id = "year",
                   treatment = "placebo",
                   outcome = "v2x_cspart")

# create matched sets
PMresults.civil.att <- PanelMatch(panel.data = panel, lag = 5, refinement.method = "CBPS.weight",
                                    match.missing = TRUE, exact.match.variables = c("un_region"),
                                    covs.formula = ~ I(lag(v2x_cspart, 1:5)) + I(lag(ln_cinc, 1:5)) + I(lag(ln_pecpp, 1:5)) + I(lag(terr_dispute, 1:5)),
                                    qoi = "att", lead = 1:5, 
                                    forbid.treatment.reversal = FALSE, use.diagonal.variance.matrix = TRUE)

# estimate the ATT
PEresults.civil.att <- PanelEstimate(panel.data = panel,
                                     sets = PMresults.civil.att,
                                     se.method = "unconditional",
                                     confidence.level = .90)

# create a plot 
gg <- data.frame(years = rep(c("t-5", "t-4", "t-3", "t-2", "t-1")),
                 estimates = c(PEresults.civil.att[["estimate"]]),
                 st_error = c(PEresults.civil.att[["standard.error"]]),
                 outcome = c(rep("Civil Society Participation (V-Dem)", 5))) %>%
  mutate(lo_ci = estimates - (1.645*st_error)) %>%
  mutate(hi_ci = estimates + (1.645*st_error)) %>% 
  mutate(significant = as.factor(ifelse(lo_ci > 0 | hi_ci < 0, 1, 0))) %>%
  glimpse()

# create Figure G.3 for the Appendix
ggplot(data = gg, aes(x = reorder(years, desc(years)), color = outcome, shape = outcome)) +
  geom_hline(aes(yintercept = 0), linetype = "dashed", linewidth = .25) +
  geom_pointrange(aes(y = estimates, ymin = lo_ci, ymax = hi_ci), size = .4, linewidth = .7, position = position_dodge(width = .2)) +
  scale_color_manual(values = c("black", "darkgray")) +
  ylim(-.05, .3) +
  theme_minimal() +
  theme(legend.position = "bottom",
        axis.text.x = element_text(color = "black", size = 10),
        axis.title.y = element_text(size = 12),
        legend.title = element_text(size = 12)) +
  labs(y = "Effect of (Placebo) Democratic Transition",
       x = "Years Until Actual Transition",
       color = "",
       shape = "")

# save plot
ggsave("figures/civil-society-placebo-att-plot.pdf", width = 6.5, height = 5, units = "in", bg = "white")
